<template>
  <el-card shadow="hover">
    <el-descriptions
      class="margin-top"
      title="开票信息"
      :column="3"
      :size="size"
      border
    />
    <el-table v-loading="listLoading" :data="InvoicingList" show-summary>
      <el-table-column
        align="center"
        label="ID"
        prop="InvoicingID"
        show-overflow-tooltip
        width="70"
      />
      <el-table-column align="center" label="发票类型" width="120">
        <template #default="{ row }">
          <el-text>{{ row.InvoicingTypeName }}</el-text>
        </template>
      </el-table-column>
      <el-table-column align="center" label="发票号" width="120">
        <template #default="{ row }">
          <el-text>{{ row.InvoicingCode }}</el-text>
        </template>
      </el-table-column>
      <el-table-column align="center" label="开票金额" width="120">
        <template #default="{ row }">
          <el-text>{{ row.InvoicingMoney }}</el-text>
        </template>
      </el-table-column>
      <el-table-column align="center" label="客户方单位">
        <template #default="{ row }">
          <el-text>{{ row.CustomerCompanyName }}</el-text>
        </template>
      </el-table-column>
      <el-table-column align="center" label="纳税人识别号">
        <template #default="{ row }">
          <el-text>{{ row.CustomerLicenseCode }}</el-text>
        </template>
      </el-table-column>
      <el-table-column align="center" label="开票方单位">
        <template #default="{ row }">
          <el-text>{{ row.MyCompanyName }}</el-text>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
  import { getInvoicingOptions } from '@/api/common'
  import { saveInvoicing } from '@/api/businessOrder'

  export default defineComponent({
    name: 'InvoicingList',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      // const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        OrderID: 0,
        CustomerID: 0,
        OrderFlowState: 1,
        listLoading: false,
        formRef: null,
        editRef: null,
        InvoicingTypes: [],
        CustomerLicenseList: [],
        MyLicenseList: [],
        InvoicingList: [],
        InvoicingForm: {
          OrderID: 0,
        },
        title: '',
      })
      const showList = (OrderInfo, list) => {
        state.OrderID = OrderInfo.OrderID
        state.CustomerID = OrderInfo.CustomerID
        getOptions()
        state.OrderFlowState = Math.abs(OrderInfo.OrderFlowState)
        state.InvoicingForm.OrderID = OrderInfo.OrderID
        state.InvoicingList = list
      }

      const getCustomerLicense = () => {
        if (state.InvoicingForm.CustomerLicenseID !== 0) {
          const Customer = state.CustomerLicenseList.find(
            (item) => item.value == state.InvoicingForm.CustomerLicenseID
          )
          state.InvoicingForm.CustomerLicenseCode = Customer.LicenseCode
        }
      }

      // const PaymentDelete = (row) => {
      //   console.log(row)
      //   if (row.OrderListID) {
      //     $baseConfirm('你确定要删除当前项吗', null, async () => {
      //       const { msg } = await deletePayment({
      //         OrderListID: row.OrderListID,
      //       })
      //       $baseMessage(msg, 'success', 'vab-hey-message-success')
      //       emit('fetch-data')
      //     })
      //   }
      // }
      const saveInvoicingok = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveInvoicing(state.InvoicingForm)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            state.InvoicingForm = {
              OrderID: 0,
            }
            emit('fetch-data')
          }
        })
      }
      const getOptions = async () => {
        state.listLoading = true

        const params = {
          CustomerID: state.CustomerID,
        }

        console.log()

        const {
          data: { InvoicingTypes, CustomerLicenseList, MyLicenseList },
        } = await getInvoicingOptions(params)
        state.InvoicingTypes = InvoicingTypes
        state.CustomerLicenseList = CustomerLicenseList
        state.MyLicenseList = MyLicenseList
        state.listLoading = false
      }
      return {
        ...toRefs(state),
        showList,
        saveInvoicingok,
        getCustomerLicense,
      }
    },
  })
</script>
